package com.hm.service.request.ydcg;

import com.alibaba.fastjson2.JSON;
import com.hm.service.entity.AuthorizationParamsBean;
import com.hm.service.http.HttpUtils;
import okhttp3.*;

import java.io.IOException;


/**
 * 数字城管获取token
 */
public class Request01 {

    private static final String url = "https://www.ydzhcg.com:7888/dex/oauth/extras/token";

    public static void main(String[] args) {
        //请求参数
        AuthorizationParamsBean paramsBean = new AuthorizationParamsBean();
        paramsBean.client_id = "10001";
        paramsBean.client_secret = "dd91a0180a";
        paramsBean.grant_type = "client_credentials";
        String reqParams = JSON.toJSONString(paramsBean);
        //构建请求体
        RequestBody body = RequestBody.create(reqParams, MediaType.get("application/json; charset=UTF-8"));
        Request request = new Request.Builder()//
                .url(url)//请求连接
                .post(body)//请求体
                .build();//构建
        //同步请求
        OkHttpClient okHttpClient = HttpUtils.getOkHttpClient();
        try (Response response = okHttpClient.newCall(request).execute()) {
            ResponseBody resBody = response.body();
            if (resBody != null) {
                String jsonString = resBody.string();
                System.out.println("ResponseBody:" + jsonString);
            } else {
                System.out.println("ResponseBody为空");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}